﻿using System.Web.Mvc;
using NUnit.Framework;

namespace SellAndBuy.Specs.Utilities
{
    public static class ActionResultExtensions
    {
        public static TModel Model<TModel>(this ActionResult result)
        {
            Assert.IsInstanceOf<ViewResult>(result);
            var viewResult = (ViewResult) result;
            Assert.IsNotNull(viewResult.ViewData.Model, "The action result does not contain a model");
            Assert.IsInstanceOf<TModel>(viewResult.ViewData.Model,
                                        "The model in the action result is not of the right type");
            return (TModel) viewResult.ViewData.Model;
        }
    }
}